//
//  LocationView.m
//  7.13苏宁框架
//
//  Created by mini on 15/7/23.
//  Copyright (c) 2015年 lovena. All rights reserved.
//

#import "LocationView.h"
#import "Citytotal.h"
#import "DetailCity.h"
#import "Common.h"
@implementation LocationView

- (IBAction)closeClick {
    if ([self.delegate respondsToSelector:@selector(closeLocationView)]) {
        [self.delegate closeLocationView];
    }
}

+(instancetype)createLocationView
{
    return [[[NSBundle mainBundle]loadNibNamed:@"LocationView" owner:nil options:nil]lastObject];
}

//加载数据
-(void)awakeFromNib
{
    _total = [NSArray array];
    _total = [Citytotal modelArrayWithFilename:@"ProvincesAndCities.plist"];
     self.tableView.delegate = self;
     self.tableView.dataSource = self;
    _count = (NSInteger)_total.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _count;
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
        static NSString *name = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:name];
    if (cell==nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:name];
    }
    if (_count == _total.count) {
        Citytotal *model1 = _total[indexPath.row];
        cell.textLabel.text = model1.State;

    }
    else
    {
         Citytotal *model1 = _total[_selectedRow];
        //NSLog(@"%d",model1.Clities.count);
        NSDictionary *dic = model1.Cities[indexPath.row];
        cell.textLabel.text = dic[@"city"];
    }
       return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //当单元格被选中时应该做两件事先是让线的位置改变，改变按钮的文字和状态，然后刷表
    if (_count == _total.count) {
        //改变线的位置和按钮状态
        CGRect frame= self.chooseView.frame;
        frame.origin.x = frame.origin.x + frame.size.width;
        self.chooseView.frame = frame;
        //改变按钮状态
        Citytotal *model = _total[indexPath.row];
        [self.chooseProvince setTitle:model.State forState:UIControlStateNormal];
        _count =  model.Cities.count;
        _selectedRow = indexPath.row;
        [self.tableView reloadData];
    }
    
    else
    {
        Citytotal *model1 = _total[_selectedRow];
        NSDictionary *dic = model1.Cities[indexPath.row];
//        NSArray *array = dic[@"city"];
//        NSLog(@"%d",array.count);
        NSString *str = dic[@"city"];
        //更换按钮文字
        [self.chooseCity setTitle:str forState:UIControlStateNormal];
        //再1.5秒后关闭视图
        [self performSelector:@selector(close2) withObject:nil afterDelay:1.5];
    }
}

-(void)close2
{
    [self.delegate closeLocationView];
}
- (IBAction)chooseProvinceClick {
    CGRect frame = self.chooseView.frame;
    frame.origin.x = 0;
    [self.chooseProvince setTitle:@"选择省" forState:UIControlStateNormal];
    self.chooseView.frame = frame;
    _count = self.total.count;
    [self.tableView reloadData];
}
- (IBAction)chooseCityClick {
    if (_count==_total.count) {
        UIAlertView * alert = [[UIAlertView alloc]initWithTitle:nil message:@"请先选择省份" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
        [alert show];
    }
}
@end
